package com.ksc.wordcount.service.server;

import com.ksc.wordcount.service.UrlTopNService;
import com.ksc.wordcount.service.impl.UrlTopNServiceImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class ThriftServer {

    public static void start(int thriftPort){
        try {
            System.out.println("thrift服务端开启....");
            TProcessor tprocessor = new UrlTopNService.Processor<UrlTopNService.Iface>(new UrlTopNServiceImpl());
            // 简单的单线程服务模型
            TServerSocket serverTransport = new TServerSocket(thriftPort);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();
        }catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}
